package com.qf.java2106.huangshengrui.hotel.filters;

import com.qf.java2106.huangshengrui.hotel.pojos.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限验证
 * @author q
 * @version V1.0
 * @date 2021/10/28
 **/
@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String uri = request.getRequestURI();
        //System.out.println("LoginFilter拦截的uri = " + uri);
        //System.out.println(request.getContextPath());
        if (uri.endsWith("/login") || uri.endsWith(".js") || uri.endsWith(".jsp") || uri.endsWith("/verifyCode")/* || uri.endsWith("/")*/ || uri.endsWith("/AjaxCheckUser")){
            chain.doFilter(request,response);
            return;
        }
        Object username = request.getSession().getAttribute("username");
        if (null!=username){
            System.out.println("当前已登陆~");
            chain.doFilter(request,response);
            return;
        }
        System.out.println("请先登陆~");
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
